package cn.edu.scau.cmi.chenbingquan.client;

import cn.edu.scau.cmi.chenbingquan.simpleFactoryMethod.MeatFactory;
import cn.edu.scau.cmi.chenbingquan.simpleFactoryMethod.domain.Meat;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

// 简单工厂模式用户类
public class SimpleFactoryClient extends Application {
	private Pane root = new Pane();
	private ComboBox<String> cb;	// 下拉框
	private Button button;		// 创建按钮
	private ListView<Text> lv;	// 文本显示列表
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) {
		
		root.setStyle("-fx-background-color: linear-gradient(to bottom right, ANTIQUEWHITE, CORNFLOWERBLUE)");
		
		cb = new ComboBox<>();
		cb.getItems().addAll("beef", "pork", "chicken");
		cb.setVisibleRowCount(3);
		cb.setPromptText("请选择生产的肉类");
		cb.setLayoutX(60);
		cb.setLayoutY(70);
		
		button = new Button("创建");
		button.setLayoutX(230);
		button.setLayoutY(70);
		
		lv = new ListView<>();
		lv.getItems().add(new Text("简单工厂模式生产的肉类行为如下："));
		lv.setPrefSize(480, 280);
		lv.setFixedCellSize(30);
		lv.setLayoutX(60);
		lv.setLayoutY(150);
		root.getChildren().addAll(cb, button, lv);
		
		Scene scene = new Scene(root, 600, 450);
		stage.setScene(scene);
		stage.getIcons().add(new Image("cn/edu/scau/cmi/chenbingquan/image/窗口左上角图标.png"));
		stage.setTitle("简单工厂模式");
		stage.setResizable(false);
		stage.show();
		
		// 事件
		// 创建按钮点击事件：产生肉类并输出信息
		button.setOnMouseClicked(e -> {
			if(cb.getValue() == null) {		// 没有选择肉类时
				showWrongStage();
			} else {
				String type = cb.getValue();
				Meat meat = MeatFactory.getMeat(type);
				Text text = new Text(meat.getInfo());
				text.setFill(Color.ORANGERED);
				lv.getItems().add(text);
			}
		});
	}
	
	// 没有选择肉类时出现错误窗口
	public void showWrongStage() {
			
		VBox vBox = new VBox(60);
		vBox.setAlignment(Pos.CENTER);
		vBox.setStyle("-fx-background-color: ANTIQUEWHITE");
			
		Text text = new Text("请先选择肉类!!");
		text.setStyle("-fx-font-size: 25; -fx-fill: orange"); 
			
		Button button = new Button("确定");
		button.setPrefSize(60, 30);
		vBox.getChildren().addAll(text, button);
			
		Stage stage = new Stage();
		Scene scene = new Scene(vBox, 300, 250);
		stage.setScene(scene);
		stage.show();
			
		// 事件
		button.setOnMouseClicked(e -> stage.close() ); 		// 关闭窗口
	}
}
